package DAO;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import to.DeterminacaoElemento;
import util.HibernateUtil;
import visao.ElementoVO;

public class DeterminacaoElementoDAO {

	/*
	public List<DeterminacaoElemento> buscaElementoPorDeterminacao(
			Integer determinacao) {

		List<DeterminacaoElemento> listaElemento = null;

		try {

			Session session = HibernateUtil.getSessionFactory().openSession();
			Criteria criteria = session
					.createCriteria(DeterminacaoElemento.class);
			criteria.createAlias("determinacao", "d");
			criteria.add(Restrictions.eq("d.codigo", determinacao));
			criteria.addOrder(Order.asc("d.codigo"));
			listaElemento = (List<DeterminacaoElemento>) criteria.list();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return listaElemento;
	}

*/

	public List<SelectItem> buscarElementoPorDeterminacao(Integer d) {
		StringBuilder sql = new StringBuilder();

		sql.append("select e.codigo, e.descricao from elemento e inner join determinacaoelemento det on det.id_elemento = e.codigo where det.id_determinacao = :param ");
		List<SelectItem> listaRetorno = new ArrayList<SelectItem>();

		try {
			
			Session session = HibernateUtil.getSessionFactory().openSession();
			SQLQuery query = session.createSQLQuery(sql.toString());
			query.setInteger("param", d);

			List lista = query.list();

			for (Object obj : lista) {

				Object[] valores = (Object[]) obj;

				SelectItem e = new SelectItem();
				e.setValue(Integer.parseInt(valores[0].toString()));
				e.setLabel(valores[1].toString());
				
				System.out.println(e.getValue());
				System.out.println(e.getLabel());

				listaRetorno.add(e);

			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		return listaRetorno;
	}
	
	
}
